#include "project.h"

namespace losor
{
	project::project( string name ):
		object( "project", name, NULL )
	{
	}
		
	project::~project()
	{
		// TODO:
	}
	
	int project::add_path( string path )
	{
		m_path.insert( path );
	}
	
	int project::add_include( string path )
	{
		m_inc.insert( path );
	}
	
	int project::rm_path( string path )
	{
		m_path.erase( path );
	}
	
	int project::rm_include( string path )
	{
		m_inc.earse( path );
	}
	
	long project::parse( string para, object *parent )
	{
		char *dir_name;
		dir *_dir;
		set<string>::iterator i;
		i = m_path.find( para );
		if( i == m_path.end() )
			i = m_inc.find( para );

		if( i != m_path.end() && i != m_inc.end() )
		{
			dir_name = strutil::filename( para.c_str() );
			rm_child( para );
			_dir = new dir( para, dir_name, this );
			add_child( _dir );
			_dir->parse();
			strutil::free( dir_name );
		}
		else
		{
			for( i = m_inc.begin(); i != m_inc.end(); ++i )
			{
				dir_name = strutil::filename( (*i).c_str() );
				rm_child( *i );
				_dir = new dir( *i, dir_name, this );
				add_child( _dir );
				_dir->parse();
				strutil::free( dir_name );
			}
			for( i = m_path.begin(); i != m_path.end(); ++i )
			{
				dir_name = strutil::filename( (*i).c_str() );
				rm_child( *i );
				_dir = new dir( *i, dir_name, this );
				add_child( _dir );
				_dir->parse();
				strutil::free( dir_name );
			}
		}
		return 0;
	}
	
	long project::write_depend( object *parent )
	{
		list<object*>::iterator i;
		for( i = m_child.begin(); i != m_child.end(); ++i )
			(*i)->write_depend( this );
		return 0;
	}

	void project::rm_child( string para )
	{
		dir *_dir;
		list<object*>::iterator i;
		for( i = m_child.begin(); i != m_child.end(); ++i )
		{
			_dir = (dir*)*i;
			if( _dir->fullpath() == para )
			{
				m_child.erase( i );
				break;
			}
		}
	}
	
}

